﻿using System;
using System.Collections.Generic;
using System.Text;
using MapgisEbp.QuickDataBase.Configuration;
using MapgisEbp.QuickDataBase;
using System.IO;
using System.Xml;
using System.Configuration;
using MapgisEbp.ORM;

namespace Logistics.DataManage.DB
{
    /// <summary>
    /// 数据库配置信息获取类型，现在直接从文件读取，后期可修改为从web注入
    /// </summary>
    public class DbConfigInfo 
    {
        private ConnectionInfo m_ConnectionInfo;

        public DbConfigInfo(String connectString,String dataServerType)
        {
            m_ConnectionInfo = new ConnectionInfo(connectString, dataServerType);
        }

        /// <summary>
        /// 获取数据库链接信息
        /// </summary>
        public ConnectionInfo ConnectionInfo
        {
            get 
            {
               return m_ConnectionInfo;
            }
        }
        /// <summary>
        /// 获取数据库操作上下文
        /// </summary>
        public IDataBaseContext DataBaseContext
        {
            get { return new DataBaseContext(ConnectionInfo); }
        }

        /// <summary>
        /// 注入数据库链接信息
        /// </summary>
        /// <param name="connInfo"></param>
        public void InjectConnectionInfo(ConnectionInfo connInfo)
        {
            m_ConnectionInfo = connInfo;      
        }

        /// <summary>
        /// 尝试获取数据库时间
        /// </summary>
        /// <param name="handler"></param>
        /// <returns></returns>
        public DateTime GetDateTimeFromDB(IDataBaseHandler handler)
        {
            string SQL = handler.DbAdapter.GetSQLGenerator().GetDataBaseTimeSQL();
            if (!String.IsNullOrEmpty(SQL))
            {
                Object oDt = handler.ExecuteScalar(SQL);
                if (oDt != null && oDt != DBNull.Value)
                {
                    return DateTime.Parse(Convert.ToString(oDt));
                }
            }
            return DateTime.Now;
        }
    }
}
